package app.server.project.main.service.db.dao.user;

import app.server.project.main.service.db.entity.user.UserIPEntity;
import app.server.project.main.service.db.mapper.user.UserIpMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class UserIpDao extends ServiceImpl<UserIpMapper, UserIPEntity> {

    @Cacheable(key = "#userId", cacheNames = "app.server.project.main.service.db.dao.UserIPEntity")
    public List<UserIPEntity> getAll(long userId, int size) {
        IPage<UserIPEntity> page = Page.of(1, size);
        var dbs = this.page(page, Wrappers.lambdaQuery(UserIPEntity.class)
                .eq(UserIPEntity::getUserId, userId)
                .orderByAsc(UserIPEntity::getLoginTime));
        var ret = new ArrayList<UserIPEntity>(dbs.getRecords());
        ret.forEach(item -> {
            item.setSynDb(true);
        });
        return ret;
    }


}
